﻿using System.Collections.Generic;
using UnityEngine;

namespace MeshPickerTool
{
    internal class MeshInfo
    {
        public List<Vector3> vertices;
        public List<int> triangles;
        public List<Vector2> uvs;
        public List<Vector3> normals;
        public List<Vector4> tangents;
        public Vector3 size;
        public Vector3 center;

        public MeshInfo()
        {
            this.vertices = new List<Vector3>();
            this.triangles = new List<int>();
            this.uvs = new List<Vector2>();
            this.normals = new List<Vector3>();
            this.tangents = new List<Vector4>();
            this.size = this.center = Vector3.zero;
        }

        public MeshInfo(Mesh mesh)
        {
            this.vertices = new List<Vector3>(mesh.vertices);
            this.triangles = new List<int>(mesh.triangles);
            this.uvs = new List<Vector2>(mesh.uv);
            this.normals = new List<Vector3>(mesh.normals);
            this.tangents = new List<Vector4>(mesh.tangents);
            Bounds bounds1 = mesh.bounds;
            this.center = bounds1.center;
            Bounds bounds2 = mesh.bounds;
            this.size = bounds2.size;
        }

        public void Add(Mesh mesh)
        {
            for (int index = 0; index < mesh.vertexCount; ++index)
            {
                this.vertices.Add(mesh.vertices[index]);
                this.uvs.Add(mesh.uv[index]);
                this.normals.Add(mesh.normals[index]);
                this.tangents.Add(mesh.tangents[index]);
            }

            int count = this.triangles.Count;
            for (int index = 0; index < mesh.triangles.Length; ++index)
            {
                this.triangles.Add(mesh.triangles[index] + count);
            }
        }

        public void Add(MeshInfo meshInfo)
        {
            for (int index = 0; index < meshInfo.vertices.Count; ++index)
            {
                this.vertices.Add(meshInfo.vertices[index]);
                this.uvs.Add(meshInfo.uvs[index]);
                this.normals.Add(meshInfo.normals[index]);
                this.tangents.Add(meshInfo.tangents[index]);
            }

            int count = this.triangles.Count;
            for (int index = 0; index < meshInfo.triangles.Count; ++index)
            {
                this.triangles.Add(meshInfo.triangles[index] + count);
            }
        }

        public void Add(Vector3 vert, Vector2 uv, Vector3 normal, Vector4 tangent)
        {
            this.vertices.Add(vert);
            this.uvs.Add(uv);
            this.normals.Add(normal);
            this.tangents.Add(tangent);
        }

        public Mesh GetMesh()
        {
            Mesh mesh = new Mesh();
            Vector3[] verticeArray = this.vertices.ToArray();
            mesh.vertices = verticeArray;
            Vector2[] uvArray = this.uvs.ToArray();
            mesh.uv = uvArray;
            Vector3[] normalArray = this.normals.ToArray();
            mesh.normals = normalArray;
            Vector4[] tangentArray = this.tangents.ToArray();
            mesh.tangents = tangentArray;
            int[] numArray = this.triangles.ToArray();
            mesh.triangles = numArray;
            return mesh;
        }

        public void MapperCube(Rect range)
        {
            if (this.uvs.Count < this.vertices.Count)
                this.uvs = new List<Vector2>(this.vertices.Count);
            int num1 = this.triangles.Count / 3;
            for (int index1 = 0; index1 < num1; ++index1)
            {
                int index2 = this.triangles[index1 * 3];
                int index3 = this.triangles[index1 * 3 + 1];
                int index4 = this.triangles[index1 * 3 + 2];
                Vector3 vector3_1 = this.vertices[index2] - this.center + this.size / 2f;
                Vector3 vector3_2 = this.vertices[index3] - this.center + this.size / 2f;
                Vector3 vector3_3 = this.vertices[index4] - this.center + this.size / 2f;
                //// ISSUE: explicit reference operation
                //((Vector3)@vector3_1).\u002Ector((float)(vector3_1.x / this.size.x), (float)(vector3_1.y / this.size.y), (float)(vector3_1.z / this.size.z));
                //// ISSUE: explicit reference operation
                //((Vector3)@vector3_2).\u002Ector((float)(vector3_2.x / this.size.x), (float)(vector3_2.y / this.size.y), (float)(vector3_2.z / this.size.z));
                //// ISSUE: explicit reference operation
                //((Vector3)@vector3_3).\u002Ector((float)(vector3_3.x / this.size.x), (float)(vector3_3.y / this.size.y), (float)(vector3_3.z / this.size.z));
                Vector3 vector3_4 = Vector3.Cross(vector3_1 - vector3_2, vector3_3 - vector3_2);
                float num2 = Mathf.Abs(Vector3.Dot(vector3_4, Vector3.right));
                float num3 = Mathf.Abs(Vector3.Dot(vector3_4, Vector3.up));
                float num4 = Mathf.Abs(Vector3.Dot(vector3_4, Vector3.forward));
                if ((double)num2 > (double)num3 && (double)num2 > (double)num4)
                {
                    this.uvs[index2] = new Vector2((float)vector3_1.z, (float)vector3_1.y);
                    this.uvs[index3] = new Vector2((float)vector3_2.z, (float)vector3_2.y);
                    this.uvs[index4] = new Vector2((float)vector3_3.z, (float)vector3_3.y);
                }
                else if ((double)num3 > (double)num2 && (double)num3 > (double)num4)
                {
                    this.uvs[index2] = new Vector2((float)vector3_1.x, (float)vector3_1.z);
                    this.uvs[index3] = new Vector2((float)vector3_2.x, (float)vector3_2.z);
                    this.uvs[index4] = new Vector2((float)vector3_3.x, (float)vector3_3.z);
                }
                else if ((double)num4 > (double)num2 && (double)num4 > (double)num3)
                {
                    this.uvs[index2] = new Vector2((float)vector3_1.x, (float)vector3_1.y);
                    this.uvs[index3] = new Vector2((float)vector3_2.x, (float)vector3_2.y);
                    this.uvs[index4] = new Vector2((float)vector3_3.x, (float)vector3_3.y);
                }

                

                // ISSUE: explicit reference operation
                // ISSUE: explicit reference operation
                // ISSUE: explicit reference operation
                // ISSUE: explicit reference operation
                // ISSUE: explicit reference operation
                // ISSUE: explicit reference operation
                this.uvs[index2] = new Vector2(range.xMin + (float)((range.xMax - range.xMin) * this.uvs[index2].x), range.yMin + (float)((range.yMax - range.yMin) * this.uvs[index2].y));
                // ISSUE: explicit reference operation
                // ISSUE: explicit reference operation
                // ISSUE: explicit reference operation
                // ISSUE: explicit reference operation
                // ISSUE: explicit reference operation
                // ISSUE: explicit reference operation
                this.uvs[index3] = new Vector2(range.xMin + (float)((range.xMax - range.xMin) * this.uvs[index3].x), range.yMin + (float)((range.yMax - range.yMin) * this.uvs[index3].y));
                // ISSUE: explicit reference operation
                // ISSUE: explicit reference operation
                // ISSUE: explicit reference operation
                // ISSUE: explicit reference operation
                // ISSUE: explicit reference operation
                // ISSUE: explicit reference operation
                this.uvs[index4] = new Vector2(range.xMin + (float)((range.xMax - range.xMin) * this.uvs[index4].x), range.yMin + (float)((range.yMax - range.yMin) * this.uvs[index4].y));
            }
        }

        public void CombineVertices(float range)
        {
            range *= range;
            for (int index1 = 0; index1 < this.vertices.Count; ++index1)
            {
                for (int index2 = index1 + 1; index2 < this.vertices.Count; ++index2)
                {
                    Vector3 vector3 = this.vertices[index1] - this.vertices[index2];
                    // ISSUE: explicit reference operation
                    int num1 = (double)vector3.sqrMagnitude < (double)range ? 1 : 0;
                    Vector2 vector2 = this.uvs[index1] - this.uvs[index2];
                    // ISSUE: explicit reference operation
                    bool flag1 = vector2.sqrMagnitude < range;
                    bool flag2 = Vector3.Dot(this.normals[index1], this.normals[index2]) > 0.999000012874603;
                    int num2 = flag1 ? 1 : 0;
                    if ((num1 & num2 & (flag2 ? 1 : 0)) != 0)
                    {
                        for (int index3 = 0; index3 < this.triangles.Count; ++index3)
                        {
                            if (this.triangles[index3] == index2)
                                this.triangles[index3] = index1;
                            if (this.triangles[index3] > index2)
                            {
                                List<int> list = this.triangles;
                                int num3 = index3;
                                int index4 = num3;
                                int num4 = list[index4];
                                int index5 = num3;
                                int num5 = num4 - 1;
                                list[index5] = num5;
                            }
                        }
                        this.vertices.RemoveAt(index2);
                        this.normals.RemoveAt(index2);
                        this.tangents.RemoveAt(index2);
                        this.uvs.RemoveAt(index2);
                    }
                }
            }
        }

        public void Reverse()
        {
            int num1 = this.triangles.Count / 3;
            for (int index = 0; index < num1; ++index)
            {
                int num2 = this.triangles[index * 3 + 2];
                this.triangles[index * 3 + 2] = this.triangles[index * 3 + 1];
                this.triangles[index * 3 + 1] = num2;
            }
            int count = this.vertices.Count;
            for (int index1 = 0; index1 < count; ++index1)
            {
                List<Vector3> list = this.normals;
                int index2 = index1;
                list[index2] = list[index2] * -1f;
                Vector4 vector4 = this.tangents[index1];
                vector4.w = -1.0f;
                this.tangents[index1] = vector4;
            }
        }

        public static Vector4 CalculateTangent(Vector3 normal)
        {
            Vector3 vector3_1 = Vector3.Cross(normal, Vector3.up);
            if (Vector3.Equals(vector3_1, Vector3.zero))
            {
                vector3_1 = Vector3.Cross(normal, Vector3.forward);
            }

            Vector3 vector3_2 = Vector3.Cross(vector3_1, normal);
            return new Vector4((float) vector3_2.x, (float) vector3_2.y, (float) vector3_2.z, 1f);
        }
    }
}
